package com.youlai.boot.modules.game.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.game.model.form.GameInfoForm;
import com.youlai.boot.modules.game.model.query.GameInfoQuery;
import com.youlai.boot.modules.game.model.vo.GameInfoVO;
import com.youlai.boot.modules.game.service.GameInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 游戏信息控制器
 *
 * @author 有来技术团队
 */
@Tag(name = "游戏信息接口")
@RestController
@RequestMapping("/api/v1/games")
@RequiredArgsConstructor
public class GameInfoController {

    private final GameInfoService gameInfoService;

    @Operation(summary = "获取游戏分页列表")
    @GetMapping("/page")
    public PageResult<GameInfoVO> getGamePage(
            @ParameterObject GameInfoQuery queryParams
    ) {
        Page<GameInfoVO> page = gameInfoService.getGamePage(queryParams);
        return PageResult.success(page);
    }

    @Operation(summary = "获取游戏详情")
    @GetMapping("/{id}")
    public Result<GameInfoVO> getGameDetail(
            @Parameter(description = "游戏ID") @PathVariable Long id
    ) {
        GameInfoVO gameInfo = gameInfoService.getGameDetail(id);
        return Result.success(gameInfo);
    }

    @Operation(summary = "新增游戏")
    @PostMapping
    public Result<Long> addGame(
            @Validated @RequestBody GameInfoForm formData
    ) {
        Long id = gameInfoService.addGame(formData);
        return Result.success(id);
    }

    @Operation(summary = "修改游戏")
    @PutMapping("/{id}")
    public Result<Boolean> updateGame(
            @Parameter(description = "游戏ID") @PathVariable Long id,
            @Validated @RequestBody GameInfoForm formData
    ) {
        boolean result = gameInfoService.updateGame(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除游戏")
    @DeleteMapping("/{ids}")
    public Result<Boolean> deleteGames(
            @Parameter(description = "游戏ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        boolean result = gameInfoService.deleteGames(idList);
        return Result.judge(result);
    }

    @Operation(summary = "获取推荐游戏列表")
    @GetMapping("/featured")
    public Result<List<GameInfoVO>> getFeaturedGames(
            @Parameter(description = "获取数量") @RequestParam(defaultValue = "6") Integer limit
    ) {
        List<GameInfoVO> games = gameInfoService.getFeaturedGames(limit);
        return Result.success(games);
    }

    @Operation(summary = "获取热门游戏列表")
    @GetMapping("/hot")
    public Result<List<GameInfoVO>> getHotGames(
            @Parameter(description = "获取数量") @RequestParam(defaultValue = "6") Integer limit
    ) {
        List<GameInfoVO> games = gameInfoService.getHotGames(limit);
        return Result.success(games);
    }

    @Operation(summary = "获取新品游戏列表")
    @GetMapping("/new")
    public Result<List<GameInfoVO>> getNewGames(
            @Parameter(description = "获取数量") @RequestParam(defaultValue = "6") Integer limit
    ) {
        List<GameInfoVO> games = gameInfoService.getNewGames(limit);
        return Result.success(games);
    }

    @Operation(summary = "获取分类游戏列表")
    @GetMapping("/category/{categoryId}")
    public Result<List<GameInfoVO>> getGamesByCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId,
            @Parameter(description = "获取数量") @RequestParam(defaultValue = "10") Integer limit
    ) {
        List<GameInfoVO> games = gameInfoService.getGamesByCategory(categoryId, limit);
        return Result.success(games);
    }

    @Operation(summary = "增加游戏下载次数")
    @PutMapping("/{id}/download")
    public Result<Boolean> incrementDownloadCount(
            @Parameter(description = "游戏ID") @PathVariable Long id
    ) {
        boolean result = gameInfoService.incrementDownloadCount(id);
        return Result.judge(result);
    }

    @Operation(summary = "更新游戏评分")
    @PutMapping("/{id}/rating")
    public Result<Boolean> updateRating(
            @Parameter(description = "游戏ID") @PathVariable Long id,
            @Parameter(description = "评分") @RequestParam Double rating
    ) {
        boolean result = gameInfoService.updateRating(id, rating);
        return Result.judge(result);
    }
} 